package com.probiz.estore.attribute.service.impl;

import java.util.List;

import net.sf.json.JSONObject;

import com.probiz.estore.attribute.dao.AttributeDao;
import com.probiz.estore.attribute.service.AttributeManager;
import com.probiz.estore.attribute.util.AttributeCodeGenerator;
import com.probiz.estore.common.model.attribute.Attribute;
import com.probiz.estore.core.service.impl.GenericManagerImpl;


/**
 * Manager implementation for Attribute, responsible for business processing, and communicate between web and persistence layer.
 */
public class AttributeManagerImpl extends GenericManagerImpl<Attribute> implements AttributeManager {

	private AttributeDao attributeDao = null;

	public void setAttributeDao(AttributeDao attributeDao) {
		this.attributeDao = attributeDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = attributeDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(Attribute entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(Attribute entity) {

	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.attribute.service.AttributeManager#getAttributeByCode(java.lang.String)
	 */
	public Attribute getAttributeByCode(String attributeCode) {
		Attribute attr = attributeDao.getAttributeByCode(attributeCode);
		return attr;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.attribute.service.AttributeManager#getAttributes(java.lang.Short)
	 */
	public List<Attribute> getAttributes(Short attributeType) {
		return attributeDao.getAttributes(attributeType);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.attribute.service.AttributeManager#getActiveAttributes(java.lang.Short)
	 */
	public List<Attribute> getActiveAttributes(Short attributeType){
		return attributeDao.getActiveAttributes(attributeType);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.attribute.service.AttributeManager#saveFully(com.probiz.estore.common.model.attribute.Attribute)
	 */
	public void saveFully(Attribute attribute){
		if(null == attribute.getAttributeId()){
			setAttributeCode(attribute);
			attribute.setSortOrder(Attribute.SORTORDER_DEFAULT);
		}
		attributeDao.save(attribute);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.attribute.service.AttributeManager#getJSONObject(com.probiz.estore.common.model.attribute.Attribute)
	 */
	public JSONObject getJSONObject(Attribute attribute){
		JSONObject attributeJSON = new JSONObject();
		attributeJSON.put("attributeId", attribute.getAttributeId());
		attributeJSON.put("attributeName", attribute.getAttributeName());
		attributeJSON.put("attributeDataType", attribute.getAttributeDataType());
		attributeJSON.put("attributeType", attribute.getAttributeType());
		attributeJSON.put("defaultValue", attribute.getDefaultValue());
		attributeJSON.put("isRequired", attribute.getIsRequired());
		attributeJSON.put("status", attribute.getStatus());
		return attributeJSON;
	}
	/**
	 * 设置属性code
	 * @param attribute
	 */
	private void setAttributeCode(Attribute attribute) {
		int tryTime = 1;
		Attribute sameAttribute = null;
		String attributeCode = "";
		do {
			attributeCode = AttributeCodeGenerator.generateAttributeCode();
			sameAttribute = attributeDao.getUniqueByProperty("attributeCode", attributeCode);
		} while ((sameAttribute != null)
				&& (tryTime < AttributeCodeGenerator.MAX_TRY_TIME.intValue()));
		attribute.setAttributeCode(attributeCode);

	}
}